import com.mongodb.*;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;

import java.net.UnknownHostException;

public class insertData {
    private static MongoClient mongoClient = null;
    private static DB db = null;
    private static GridFS gridFS = null;
    public static void main(String[] args) throws UnknownHostException, FileNotFoundException {

//        MongoClient client = new MongoClient("127.0.0.1:27017");
//        DB db = client.getDB("Hypertension");
//        DBCollection collection = db.getCollection("TestCnkiPaper");
//        BasicDBObject obj = new BasicDBObject();
//
//        File csv = new File("/Users/xiexiaohao/Desktop/paperInfo.csv");
//        BufferedReader br = null;
//        try {
//            br = new BufferedReader(new FileReader(csv));
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
//
//        String line = "";
//        try {
//            while ((line = br.readLine()) != null) {
//                System.out.println(line);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        List<String> files = new ArrayList<String>();
//        File file = new File("/Users/xiexiaohao/PycharmProjects/pythonProject/zhihu/zhihu/spiders/PDF1");
//        File[] tempList = file.listFiles();
//
//        for (int i = 0; i < tempList.length; i++) {
//            if (tempList[i].isFile()) {
//                System.out.println(tempList[i].toString());
//                files.add(tempList[i].toString());
//                //System.out.println(tempList[i].getName());
//            }
//        }

        File[] files = getFiles("/Users/xiexiaohao/Desktop/PDF");
        mongoClient = new MongoClient("114.67.200.39:27817");
        db = mongoClient.getDB("Hypertension");
        gridFS = new GridFS(db);

        for(File file: files) {
            save(new FileInputStream(file), file.getName());
        }
        List<GridFSDBFile> res = getByFileName();
        int i = 0;
        for(GridFSDBFile fi : res) {
            InputStream is = fi.getInputStream();
            i += 1;
            System.out.println(i);
        }




    }

    public static File[] getFiles(String path) {
        //List<String> files = new ArrayList<String>();

        File file = new File(path);
        File[] tempList = file.listFiles();

        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                System.out.println(tempList[i].toString());
                //files.add(tempList[i].toString());
                //System.out.println(tempList[i].getName());
            }
        }
        return tempList;
    }

    public static void save(InputStream in, String fileName) {
        DBObject query = new BasicDBObject("filename", fileName);
        GridFSDBFile gridFSDBFile = gridFS.findOne(query);
        if (gridFSDBFile == null) {
            GridFSInputFile gridFSInputFile = gridFS.createFile(in);
            //gridFSInputFile.setId(id);
            gridFSInputFile.setFilename(fileName);
            gridFSInputFile.save();
        }
    }

    public static GridFSDBFile getById(Object id){
        DBObject query  = new BasicDBObject("_id", id);
        GridFSDBFile gridFSDBFile = gridFS.findOne(query);

        return gridFSDBFile;
    }

    public static GridFSDBFile getByFileName(String fileName) throws IOException {
        DBObject query  = new BasicDBObject("filename", fileName);
        GridFSDBFile gridFSDBFile = gridFS.findOne(query);
        gridFSDBFile.getFilename();
//        InputStream is = gridFSDBFile.getInputStream();
//        int size = is.available();
//        byte[] bytes = new byte[size];
//        is.read(bytes);
//        is.close();
//        BASE64Encoder encoder = new BASE64Encoder();
//        String base64String = encoder.encode(bytes);
        return gridFSDBFile;
    }

    public static List<GridFSDBFile> getByFileName(){
        DBObject query  = new BasicDBObject();
        List<GridFSDBFile> res = gridFS.find(query);
        return res;
    }

}
